#!/usr/bin/python
#coding=utf-8
import ConfigParser
import codecs
import sys
reload(sys)
CONFIG_NAME = 'FuckSenWord.ini'

MAINFRAME_CONFIG = {'MainFrame': {
    'max': 'bool',
    'top': 'int',
    'left': 'int',
    'height': 'int',
    'width': 'int',
    }}

TRANSFORM_CONFIG = {'Transform': {
    'mode': 'int',
    'lineLength': 'int',
    'autoTypeset': 'bool',
    'fontQualifier': 'int',
    }}

FONT_CONFIG = {'Font': {
    'face': 'str',
    'size': 'int',
    'style': 'int',
    'weight': 'int',
    }}

cfg = ConfigParser.ConfigParser()
cfg.readfp(codecs.open(CONFIG_NAME, "rb", "utf-8-sig"))

class ConfigBase(dict):
    def __init__(self):
        (self.section, options) = self._options.items()[0]
        for (option, type) in options.items():
            if type == 'str':
                self[option] = cfg.get(self.section, option)
            elif type == 'int':
                self[option] = cfg.getint(self.section, option)
            elif type == 'bool':
                self[option] = cfg.getboolean(self.section, option)

    def write(self):
        for (option, value) in self.items():
            cfg.set(self.section, option, value)
        fp = open(CONFIG_NAME, 'wb')
        sys.setdefaultencoding('utf-8')
        cfg.write(fp)
        fp.close()
        cfg.readfp(codecs.open(CONFIG_NAME, 'r', 'utf-8-sig'))

class MainFrameConfig(ConfigBase):
    _options = MAINFRAME_CONFIG

class TransformConfig(ConfigBase):
    _options = TRANSFORM_CONFIG

class FontConfig(ConfigBase):
    _options = FONT_CONFIG
